package memleak.test.com.testlist;

import android.app.Activity;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.annotation.UiThread;
import android.util.Log;


import com.gzgd.log.LogUtils;

/**
 * Created by runrun on 2017/2/13.
 */

public class LiveApplication extends Application implements Application.ActivityLifecycleCallbacks {


    private static final String TAG = "LiveApplication";
    protected static LiveApplication mContext;
    private static long mStart;

    private DefaultConfig mDefaultConfig;
    private boolean isForeground;


    public static LiveApplication getGlobalContext() {
        return mContext;
    }

    public static void count(long time, String msg) {
        Log.i(TAG, "pay time : " + msg + " == " + (time - mStart));
    }

    protected String getAppName() {
        return "live";
    }

    @Override
    public void onCreate() {
        super.onCreate();
        LogUtils.init(this, true, getAppName());
        LogUtils.i(TAG, "Application init...begin");
        mStart = System.currentTimeMillis();
        mContext = this;


        initSpecialData();
        initDebug();
        registerActivityLifecycleCallbacks(this);
        register();
        LogUtils.i(TAG, "Application init..over .");
    }


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        Log.i(TAG, "attachBaseContext");
    }

    private void initDebug() {

    }

    private void enabledStrictMode() {

        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                .detectAll()
                .penaltyLog()
                .penaltyDeath()
                .build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                .detectLeakedSqlLiteObjects()
                .detectLeakedClosableObjects()
                .penaltyLog()
                .penaltyDeath()
                .build());
    }

    protected void initSpecialData() {
//        DataManager dm = DataManager.getInstance();
//        dm.init(this);
    }


    /**
     * 适配播放
     */
    public void adjustPlay(VideoBean bean) {

//        mMainPresenter.getLivePlay(bean);
    }

    /**
     * 适配应用或者互动栏目的跳转
     *
     * @param bean
     */
    public boolean adjustJump(VideoBean bean) {
        return true;
//       mMainPresenter.directJump(bean);
    }



    public void changeChannelPos(int position) {
        MainActivity.getinstace().changeChannelPos(position);
    }

    public void changePagePos(int position)
    {
        MainActivity.getinstace().changePagePos(position);
    }

    public void turnPagePos(int position) {
        MainActivity.getinstace().changePagePos(position);
    }

    public boolean isFlagUpdateData() {
        return false;
    }

    public void setFlagUpdateData(boolean flagUpdateData) {

    }

    public boolean isSwitchChannel() {
    //    return mMainPresenter.isSwitchChannel();
        return  false ;
    }

    protected DefaultConfig loadConfig() {
        return new DefaultConfig();
    }

    public int getPagePos() {
        return MainActivity.getinstace().getPagePos();
    }

    public VideoBean getCurVideoBean() {
        return MainActivity.getinstace().getCurVideoBean();
    }

    public int getChannelPos() {
        return MainActivity.getinstace().getChannelPos();
    }

    public int getPrePagePos() {
        return MainActivity.getinstace().getPrePagePos();
    }

    public int getPreChannelPos() {
        return MainActivity.getinstace().getPreChannelPos();
    }


    public String getCategoryId() {
        return MainActivity.getinstace().getCategoryId();
    }

    @UiThread
    public synchronized DefaultConfig getConfig() {
        if (mDefaultConfig == null) {
            mDefaultConfig = loadConfig();
        }
        return mDefaultConfig;
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        unRegister();
    }

    private void register() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(Constant.Protocol.PROTOCOL_AUTH_ACTION);
        registerReceiver(mReceiver, filter);
    }

    private void unRegister() {
        unregisterReceiver(mReceiver);
    }

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Constant.Protocol.PROTOCOL_AUTH_ACTION.equals(action)) {
                boolean isAuth = intent.getBooleanExtra("auth", false);
                if (!isAuth) {
                    LogUtils.i(TAG, " device auth failed !");
                    return;
                }
                LogUtils.i(TAG, " device auth success !");
//                UrlManager.updateUrl(context.getApplicationContext());
            }


        }
    };




    public boolean isForeground() {
        return isForeground;
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
    }

    @Override
    public void onActivityStarted(Activity activity) {
    }

    @Override
    public void onActivityResumed(Activity activity) {
        isForeground = true;
    }

    @Override
    public void onActivityPaused(Activity activity) {
        isForeground = false;
    }

    @Override
    public void onActivityStopped(Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
    }


}
